// ==========================================================================
// gin-sns自动生成控制器相关代码，只生成一次，按需修改,再次生成不会覆盖.
// date：2021-02-21 13:45:54 +0800 CST
// path: app/controller/module/favorite_api.go
// author：jon
// ==========================================================================
package module

import (
	"gin-sns/app/ginframe/response"
	"gin-sns/app/ginframe/tools"
	"gin-sns/app/model"
	favoriteModel "gin-sns/app/model/module/favorite"
	"gin-sns/app/service/module"

	"github.com/gin-gonic/gin"
	//"gin-sns/app/ginframe/utils/gconv"
)

var FavoriteApi = newFavoriteApi()

func newFavoriteApi() *favoriteApi {
	return &favoriteApi{}
}

type favoriteApi struct {
}

//列表分页数据
func (c *favoriteApi) ListAjax(ctx *gin.Context) {
	req := new(favoriteModel.SelectPageReq)
	//获取参数
	if err := ctx.ShouldBind(req); err != nil {
		response.ErrorResp(ctx).SetMsg(err.Error()).Log("favorite管理", req).WriteJsonExit()
		return
	}
	rows := make([]favoriteModel.Entity, 0)
	result, page, err := module.FavoriteService.SelectListByPage(req)

	if err == nil && len(result) > 0 {
		rows = result
	}

	response.BuildTable(ctx, page.Total, rows).WriteJsonExit()
}

//新增页面保存
func (c *favoriteApi) AddSave(ctx *gin.Context) {
	req := new(favoriteModel.AddReq)
	//获取参数
	if err := ctx.ShouldBind(req); err != nil {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Add).SetMsg(err.Error()).Log("收藏新增数据", req).WriteJsonExit()
		return
	}

	id, err := module.FavoriteService.AddSave(req, ctx)

	if err != nil || id <= 0 {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Add).Log("收藏新增数据", req).WriteJsonExit()
		return
	}
	response.SucessResp(ctx).SetData(id).Log("收藏新增数据", req).WriteJsonExit()
}

//修改页面保存
func (c *favoriteApi) EditSave(ctx *gin.Context) {
	req := new(favoriteModel.EditReq)
	//获取参数
	if err := ctx.ShouldBind(req); err != nil {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Edit).SetMsg(err.Error()).Log("收藏修改数据", req).WriteJsonExit()
		return
	}

	rs, err := module.FavoriteService.EditSave(req, ctx)

	if err != nil || rs <= 0 {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Edit).Log("收藏修改数据", req).WriteJsonExit()
		return
	}
	response.SucessResp(ctx).SetBtype(model.Buniss_Edit).Log("收藏修改数据", req).WriteJsonExit()
}

//删除数据
func (c *favoriteApi) Remove(ctx *gin.Context) {
	req := new(model.RemoveReq)
	//获取参数
	if err := ctx.ShouldBind(req); err != nil {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Del).SetMsg(err.Error()).Log("收藏删除数据", req).WriteJsonExit()
		return
	}

	rs := module.FavoriteService.DeleteRecordByIds(req.Ids)

	if rs > 0 {
		response.SucessResp(ctx).SetBtype(model.Buniss_Del).Log("收藏删除数据", req).WriteJsonExit()
	} else {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Del).Log("收藏删除数据", req).WriteJsonExit()
	}
}

// 是否收藏了
func (c *favoriteApi) GetFavorited(ctx *gin.Context) {
	user := module.UsertokenService.GetCurrent(ctx)
	if user == nil {
		response.ErrorResp(ctx).SetMsg(tools.ErrorNotLogin.Error()).WriteJsonExit()
		return
	}
	entityType := ctx.GetInt("entityType")
	entityId := ctx.GetInt64("entityId")
	if user == nil || entityType == 0 || entityId <= 0 {
		response.ErrorResp(ctx).SetData(false).WriteJsonExit() //"favorited",
		return
	} else {
		tmp := module.FavoriteService.GetBy(user.Id, entityType, entityId)

		response.SucessResp(ctx).SetData(tmp != nil).WriteJsonExit() //Put("favorited", tmp != nil)
		return
	}
}

// 取消收藏
func (c *favoriteApi) PostDelete(ctx *gin.Context) {
	user := module.UsertokenService.GetCurrent(ctx)

	entityType := ctx.GetInt("entityType")
	entityId := ctx.GetInt64("entityId")

	if user == nil {
		response.ErrorResp(ctx).SetMsg(tools.ErrorNotLogin.Error()).WriteJsonExit()
		return
	}
	tmp := module.FavoriteService.GetBy(user.Id, entityType, entityId)
	if tmp != nil {
		module.FavoriteService.DeleteRecordById(tmp.Id)
	}
	response.SucessResp(ctx).SetData(true).WriteJsonExit()
	return
}

// 用户收藏
func (c *favoriteApi) GetFavorites(ctx *gin.Context) {
	user := module.UsertokenService.GetCurrent(ctx)

	userName := ctx.GetString("user")
	pageNum := ctx.GetInt("page")  //default 1
	pageSize := ctx.GetInt("size") //default 20

	// 用户必须登录
	if user == nil {
		response.ErrorResp(ctx).SetMsg(tools.ErrorNotLogin.Error()).WriteJsonExit()
		return
	}

	tUser := module.UserService.GetByUsername(userName)
	if tUser == nil {
		tUser = user //see myself
	}

	req := new(favoriteModel.SelectPageReq)
	req.UserId = tUser.Id
	req.PageNum = pageNum
	req.PageSize = pageSize

	rows := make([]favoriteModel.Entity, 0)
	result, page, err := module.FavoriteService.SelectListByPage(req)
	if err == nil && len(result) > 0 {
		rows = result
	}

	response.BuildTable(ctx, page.Total, rows).WriteJsonExit()
	return

}
